package com.tpc.client.systeme.core.datasource;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.tpc.client.modules.core.organisation.service.OrganisationService;
import com.tpc.client.modules.core.organisation.service.OrganisationServiceAsync;
import com.tpc.client.systeme.core.Systeme;
import com.tpc.dao.extend.Tag;

/**
 * @version 0.0.1
 * @author frederic.fortin
 * @modified 2011-07-15
 */
public class OrganisationCategoryDataSource extends GwtRpcDataSource {

	Systeme systeme = null;

    public OrganisationCategoryDataSource(String strID) {
            setID(strID);            
            
            DataSourceIntegerField categoryId = new DataSourceIntegerField("category_id");
            categoryId.setPrimaryKey(Boolean.TRUE);
            DataSourceTextField categoryName = new DataSourceTextField("category_name", " ");

            setFields(categoryId,categoryName);

    }

    public void setparams(Systeme sysParam) {
    	systeme = sysParam;
    }
    
    @Override
    protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
        OrganisationServiceAsync service = GWT.create(OrganisationService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint(systeme.getPageUrl()+"/com.tpc.organisation/orgloader");
        service.loadListCategory(new AsyncCallback<Tag[]> () {
            public void onFailure (Throwable caught) {      	
                response.setStatus (RPCResponse.STATUS_FAILURE);
                processResponse (requestId, response);                
            }
            public void onSuccess (Tag[] result) {
                ListGridRecord[] list = null;
                if (result != null) {
                    list = new ListGridRecord[result.length];
                    for (int i = 0; i < result.length; i++) {
                        list[i] = new ListGridRecord();
                        list[i].setAttribute("category_id", result[i].getId());
                        list[i].setAttribute("category_name", result[i].getName());
                    }
                }
                response.setData(list);
                processResponse(requestId, response);
            }
        });

    }

    @Override
    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
        //@TODO : fdsa
    }

    @Override
    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

    @Override
    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

}
